#include "LoadState.h"

/*
File: MainMenuState.cpp
Author: Zachary Murray
Purpose: State for the main menu
*/

#include "MainMenuState.h"
#include "../SGD Wrappers/SGD_GraphicsManager.h"
#include "../SGD Wrappers/SGD_InputManager.h"
#include "../SGD Wrappers/SGD_AudioManager.h"
#include "../SGD Wrappers/SGD_Event.h"
#include "Game.h"
#include "BitmapFont.h"
#include "Menu.h"
#include "GamePlayState.h"
#include "OptionsState.h"
#include "HowToState.h"
#include "CreditsState.h"
#include "MenuCursor.h"
#include <vector>
#include <fstream>
#include <sstream>
#include <istream>
#include "../TinyXML/tinyxml.h"

LoadState* LoadState::GetInstance()
{
	static LoadState instance;
	return &instance;
}

void LoadState::Enter()
{
	
	SGD::GraphicsManager::GetInstance()->SetClearColor({ 0, 0, 0 });

	menu = new Menu;
	menu->Initialize("resources/xml/loadmenu.xml");
	std::vector<SGD::Point> p;
	for (unsigned int i = 0; i < menu->menuItems.size(); i++)
		p.push_back(menu->menuItems[i].GetPosition());
	cursor = new MenuCursor;
	cursor->Initialize(p);
	cursor->SetOffset({ -115, 15 });
	cursor->SetSize({ 5, 5 });
	cursor->SetPosition(cursor->GetPositions()[0]);

	loadOptions = new Menu;
	loadOptions->Initialize("resources/xml/loaddlg.xml");
	std::vector<SGD::Point> lP;
	for (unsigned int i = 0; i < loadOptions->menuItems.size(); i++)
		lP.push_back(loadOptions->menuItems[i].GetPosition());
	loadCurs = new MenuCursor;
	loadCurs->Initialize(lP);
	loadCurs->SetOffset({ -115, 15 });
	loadCurs->SetSize({ 5, 5 });
	loadCurs->SetPosition(loadCurs->GetPositions()[0]);

	TiXmlDocument doc;
	doc.LoadFile("resources/xml/GameSave.xml");
	TiXmlElement * s1 = doc.RootElement()->FirstChildElement();
	menu->menuItems[0].buttons[0].SetData(s1->Attribute("used"));
	TiXmlElement*s2 = s1->NextSiblingElement();
	menu->menuItems[1].buttons[0].SetData(s2->Attribute("used"));
	TiXmlElement*s3 = s2->NextSiblingElement();
	menu->menuItems[2].buttons[0].SetData(s3->Attribute("used"));

	background = SGD::GraphicsManager::GetInstance()->LoadTexture("resources/images/shadowmancer_menu.png");

}

void LoadState::Exit()
{
	SGD::GraphicsManager::GetInstance()->UnloadTexture(background);

	loadDLG = false;
	cursPos = 0;
	delete menu;
	menu = nullptr;
	delete cursor;
	cursor = nullptr;

	delete loadOptions;
	loadOptions = nullptr;
	delete loadCurs;
	loadCurs = nullptr;
}

bool LoadState::Input()
{
	SGD::InputManager* input = SGD::InputManager::GetInstance();

	if (input->IsAnyKeyPressed())
		mouseInput = false;
	else if (input->GetMouseMovement() != SGD::Vector{ 0, 0 })
		mouseInput = true;

	if (!loadDLG)
	{
		if (input->IsKeyPressed(SGD::Key::Down) == true
			|| input->IsDPadPressed(0, SGD::DPad::Down))
		{
			SGD::Event e = { "MENUMOVE" };
			e.SendEventNow(nullptr);
			cursPos++;
			if ((unsigned int)cursPos > cursor->GetPositions().size() - 1)
				cursPos = 0;
		}
		if (input->IsKeyPressed(SGD::Key::Up) == true
			|| input->IsDPadPressed(0, SGD::DPad::Up))
		{
			SGD::Event e = { "MENUMOVE" };
			e.SendEventNow(nullptr);
			cursPos--;
			if (cursPos < 0)
				cursPos = cursor->GetPositions().size() - 1;
		}
		if (input->IsKeyPressed(SGD::Key::Enter) == true
			|| input->IsKeyPressed(SGD::Key::LButton) == true
			|| input->IsButtonPressed(0, 0))
		{
			if (cursPos == 0)
			{
				if ("empty" != menu->menuItems[0].buttons[0].GetData())
				{
					saveToLoad = menu->menuItems[0].buttons[0].GetData();
					loadDLG = true;
				}
			}
			else if (cursPos == 1)
			{
				if ("empty" != menu->menuItems[1].buttons[0].GetData())
				{
					saveToLoad = menu->menuItems[1].buttons[0].GetData();
					loadDLG = true;
				}
			}
			else if (cursPos == 2)
			{
				if ("empty" != menu->menuItems[2].buttons[0].GetData())
				{
					saveToLoad = menu->menuItems[2].buttons[0].GetData();
					loadDLG = true;
				}
			}
			else if (cursPos == 3)
			{
				Game::GetInstance()->ChangeState(MainMenuState::GetInstance());
				return true;
			}
		}
	}
	else
	{
		if (input->IsKeyPressed(SGD::Key::Right) == true
			|| input->IsDPadPressed(0, SGD::DPad::Right))
		{
			SGD::Event e = { "MENUMOVE" };
			e.SendEventNow(nullptr);
			loadCPos++;
			if (loadCPos > 2)
				loadCPos = 0;
		}
		else if (input->IsKeyPressed(SGD::Key::Left) == true
			|| input->IsDPadPressed(0, SGD::DPad::Left))
		{
			SGD::Event e = { "MENUMOVE" };
			e.SendEventNow(nullptr);
			loadCPos--;
			if (loadCPos < 0)
				loadCPos = 2;
		}
		if (0 == loadCPos && (input->IsKeyPressed(SGD::Key::Enter)
			|| input->IsKeyPressed(SGD::Key::LButton)))
		{
			SGD::Event e = { "MENUSELECT" };
			e.SendEventNow(nullptr);
			Game::GetInstance()->ChangeState(GamePlayState::GetInstance(cursPos));
			return true;
		}
		else if (1 == loadCPos && (input->IsKeyPressed(SGD::Key::Enter) 
			|| input->IsKeyPressed(SGD::Key::LButton)))
		{
			SGD::Event e = { "MENUSELECT" };
			e.SendEventNow(nullptr);
			TiXmlDocument doc;
			if (doc.LoadFile("resources/xml/GameSave.xml"))
			{
				TiXmlElement*root = doc.RootElement();
				TiXmlElement*save = root->FirstChildElement();
				for (int i = 0; i < cursPos; i++)
					save = save->NextSiblingElement();
				menu->menuItems[cursPos].buttons[0].SetData("empty");
				save->SetAttribute("used", "empty");
				save->RemoveChild(save->FirstChildElement());
				save->RemoveChild(save->FirstChildElement());
				save->RemoveChild(save->FirstChildElement());
				save->RemoveChild(save->FirstChildElement());
				doc.SaveFile();
			}
			loadDLG = false;
		}
		else if ((2 == loadCPos && (input->IsKeyPressed(SGD::Key::Enter)
			|| input->IsKeyPressed(SGD::Key::LButton))) || input->IsKeyPressed(SGD::Key::Escape))
		{
			loadDLG = false;
			loadCPos = 0;
		}
	}



	return true;
}

void LoadState::Update(float elapsedTime)
{
	SGD::InputManager* input = SGD::InputManager::GetInstance();

	if (!loadDLG)
	{
		if (!mouseInput)
			cursor->SetPosition(cursor->GetPositions()[cursPos]);
		else
		{
			SGD::Rectangle rect{
				input->GetMousePosition(), SGD::Size{ 1, 1 } };
			for (unsigned int i = 0; i < menu->menuItems.size(); i++)
			{
				if (menu->menuItems[i].buttons[0].GetRect().IsIntersecting(rect))
				{
					if (cursPos != i)
					{
						SGD::Event e = { "MENUMOVE" };
						e.SendEventNow(nullptr);
					}
					cursor->SetPosition(cursor->GetPositions()[i]);
					cursPos = i;
				}
			}
		}
		menu->Update(elapsedTime);
	}
	else
	{
		if (!mouseInput)
			loadCurs->SetPosition(loadCurs->GetPositions()[loadCPos]);
		else
		{
			SGD::Rectangle rect{
				input->GetMousePosition(), SGD::Size{ 1, 1 } };
			for (unsigned int i = 0; i < loadOptions->menuItems.size(); i++)
			{
				if (loadOptions->menuItems[i].buttons[0].GetRect().IsIntersecting(rect))
				{
					if (loadCPos != i)
					{
						SGD::Event e = { "MENUMOVE" };
						e.SendEventNow(nullptr);
					}
					loadCurs->SetPosition(loadCurs->GetPositions()[i]);
					loadCPos = i;
				}
			}
		}
		loadOptions->Update(elapsedTime);
	}
}

void LoadState::Render()
{
	SGD::GraphicsManager::GetInstance()->DrawTexture(background, {}, {}, {}, {}, { 1, 1 });

	BitmapFont* font = Game::GetInstance()->GetFont();
	SGD::InputManager* input = SGD::InputManager::GetInstance();
	font->Draw("Load Game", 5, 5, 1.0f, { 255, 255, 255 });
	
	if (!loadDLG)
	{
		menu->Render();
		cursor->Render();
	}
	else
	{
		loadOptions->Render();
		loadCurs->Render();
	}
}